package com.auction.system.action.auction;

import com.auction.system.base.BaseResponse;
import com.auction.system.constant.SystemConstant;
import com.auction.system.domain.dto.user.UserInfo;
import com.auction.system.domain.query.user.UserInfoQuery;
import com.auction.system.enums.EnumReturnResult;
import com.auction.system.service.user.IUserInfoService;
import com.auction.system.utils.StringUtil;
import com.auction.system.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminInfoAction
 * @Author 王伟学
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/userInfo")
public class UserInfoAction {

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 进入管理员列表页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return "userInfo/list";
    }

    /**
     * 获取用户数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public UserInfoQuery getList(UserInfoQuery query) {
        try {
            query = userInfoService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取用户列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取用户数据异常");
        }
        return query;
    }

    /**
     * 进入注册页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String preAdd(){
        return "userInfo/add";
    }

    /**
     * 新增用户
     * @param userInfo
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated UserInfo userInfo, BindingResult result){
        BaseResponse response = new BaseResponse();
        try{
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("UserInfo-add参数有误");
                log.info("UserInfo-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            //校验学号是否被注册
            UserInfoQuery userInfoQuery = new UserInfoQuery();
            userInfoQuery.setStudentCode(userInfo.getStudentCode());
            if(StringUtil.isNotEmpty(userInfoService.getList(userInfoQuery))){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("该学号已被注册");
                return response;
            }
            //记录注册时间
            userInfo.setCreateTime(new Date());
            //信誉分默认100分
            userInfo.setCreditScore(100);
            userInfoService.add(userInfo);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("注册用户成功");
        }catch (Exception e){
            log.error("注册用户异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("注册用户失败");
        }
        return response;
    }

    /**
     * 修改个人信息
     * @return
     */
    @RequestMapping(value = "/editInfo",method = RequestMethod.GET)
    public ModelAndView perEditInfo(){
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = userInfoService.get(Long.parseLong(UserUtil.getUserId()));
        mav.addObject("userInfo",userInfo);
        mav.setViewName("userInfo/edit");
        return mav;
    }

    /**
     * 展示个人信息
     * @return
     */
    @RequestMapping(value = "/view",method = RequestMethod.GET)
    public ModelAndView view(Long id){
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = userInfoService.get(id);
        mav.addObject("userInfo",userInfo);
        mav.setViewName("userInfo/view");
        return mav;
    }

    /**
     * 进入用户信息修改页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView preEdit(Long id){
        ModelAndView mav = new ModelAndView();
        try{
            UserInfo userInfo = userInfoService.get(id);
            mav.addObject("userInfo",userInfo);
            mav.setViewName("userInfo/edit");
        }catch (Exception e){
            log.error("获取用户详情失败",e);
            mav.addObject(SystemConstant.ERROR_KEY,"系统异常");
            mav.setViewName("userInfo/list");
        }
        return mav;
    }

    /**
     * 修改用户信息
     * @param userInfo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public BaseResponse edit(@Validated UserInfo userInfo, BindingResult result){
        BaseResponse response = new BaseResponse();
        try{
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("UserInfo-edit参数有误");
                log.info("UserInfo-edit参数有误："+result.getAllErrors().toString());
                return response;
            }
            userInfoService.edit(userInfo);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("修改用户信息成功");
        }catch (Exception e){
            log.error("修改用户信息失败",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("修改用户信息失败");
        }
        return response;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public BaseResponse delete(Long id){
        BaseResponse response = new BaseResponse();
        try{
            userInfoService.delete(id);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("删除用户成功");
        }catch (Exception e){
            log.error("删除用户异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("删除用户失败");
        }
        return response;
    }

    /**
     * 进入修改密码页面
     * @return
     */
    @RequestMapping(value = "/editpassword",method = RequestMethod.GET)
    public ModelAndView preEditPassword(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("id", UserUtil.getUserId());
        mav.setViewName("userInfo/editpassword");
        return mav;
    }

    /**
     * 修改密码
     * @param id
     * @param oldPassword
     * @param newPaword
     * @param confirmPassword
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editpassword",method = RequestMethod.POST)
    public BaseResponse editPassword(Long id,String oldPassword, String newPaword, String confirmPassword){
        BaseResponse response = new BaseResponse();
        try{
            if(!newPaword.equals(confirmPassword)){
                response.setMsg("两次输入的密码不一致");
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                return response;
            }
            UserInfoQuery userInfoQuery = new UserInfoQuery();
            userInfoQuery.setId(id);
            userInfoQuery.setUserPassword(oldPassword);
            List<UserInfo> userInfoList = userInfoService.getList(userInfoQuery);
            if(StringUtil.isNotEmpty(userInfoList)){
                UserInfo userInfo = userInfoList.get(0);
                userInfo.setUserPassword(newPaword);
                userInfoService.edit(userInfo);
                response.setMsg("修改密码成功");
                response.setCode(EnumReturnResult.RETURN_200.getCode());
                return response;
            }else{
                response.setMsg("原密码错误");
                response.setCode(EnumReturnResult.RETURN_404.getCode());
            }
        }catch (Exception e){
            response.setMsg("修改密码异常");
            response.setCode(EnumReturnResult.RETURN_404.getCode());
        }
        return response;
    }

    /**
     * 进入修改信誉积分页面
     * @return
     */
    @RequestMapping(value = "/editCreditScore",method = RequestMethod.GET)
    public ModelAndView perEditCreditScore(Long id){
        ModelAndView mav = new ModelAndView();
        mav.addObject("userInfo", userInfoService.get(id));
        mav.setViewName("userInfo/editCreditScore");
        return mav;
    }

    /**
     * 修改信誉积分
     * @param userInfo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editCreditScore",method = RequestMethod.POST)
    public BaseResponse editCreditScore(UserInfo userInfo){
        BaseResponse response = new BaseResponse();
        try{
            userInfoService.edit(userInfo);
            response.setMsg("修改信誉积分成功");
            response.setCode(EnumReturnResult.RETURN_200.getCode());
        }catch (Exception e){
            response.setMsg("修改信誉积分失败");
            response.setCode(EnumReturnResult.RETURN_404.getCode());
        }
        return response;
    }

}
